﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EasyBuildingCMS.BLL.CoreEngine;
using Geekees.Common.Controls;

namespace EasyBuildingCMS.BuildingBlocks.Core.Navigation.TreeView
{
    public partial class SitePageManager : System.Web.UI.UserControl
    {
        protected string ASTreeViewVersion
        {
            get
            {
                return typeof(ASTreeView).Assembly.GetName().Version.ToString();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadTreeView();
            }

        }

        protected void SiteMapTreeView_SelectedNodeChanged(object src, ASTreeViewNodeSelectedEventArgs e)
        {
            Default page = (Default)this.Page;
            if (BLL.Helper.ToInt(e.NodeValue)!=null){
                BLL.Globals.CurrentPage = e.NodeValue;
                page.LoadPageContent((int)(BLL.Helper.ToInt(e.NodeValue)));
            }
        }

        private void InitializeComponent()
        {
      
            this.siteMapTreeView.ContextMenu.MenuItems.Add(new ASContextMenuItem("Add Page", "editPage('/Admin/AddEditViewPage.aspx?mode=Insert&parentPageID='+" + this.siteMapTreeView.ContextMenuClientID + ".getSelectedItem().parentNode.getAttribute('treeNodeValue')" + "+'&parentPageText='+" + this.siteMapTreeView.ContextMenuClientID + ".getSelectedItem().innerHTML" + "); return false;", "otherevent"));
            this.siteMapTreeView.ContextMenu.MenuItems.Add(new ASContextMenuItem("Edit Page", "editPage('/Admin/AddEditViewPage.aspx?mode=Edit&pageID='+" + this.siteMapTreeView.ContextMenuClientID + ".getSelectedItem().parentNode.getAttribute('treeNodeValue')" + "); return false;", "otherevent"));
            this.siteMapTreeView.ContextMenu.MenuItems.Add(new ASContextMenuItem("Set As HomePage", "setHomePage(" + this.siteMapTreeView.ContextMenuClientID + ".getSelectedItem().parentNode.getAttribute('treeNodeValue')" +"); return false;", "otherevent"));
        }

        protected void SetHomePageButton_Click(object sender, EventArgs e)
        {
            string value = this.HomePageIDHiddenField.Value;
            PageRepository pageRepository = new PageRepository();

            pageRepository.UpdateHomePage((int)(BLL.Helper.ToInt(value)));
        }

        protected void DragAndDropCompleted_Click(object sender, EventArgs e)
        {
            string dragedPage = this.CurrentNodeValue.Value;
            string newParentPage = this.ParentNodeValue.Value;

            ASTreeViewNode node = this.siteMapTreeView.FindByValue(newParentPage);
            if (node == null)
            {
                node = this.siteMapTreeView.RootNode;
            }
            List<ASTreeViewNode> children = node.ChildNodes;

            int draggedPageID = (int)(BLL.Helper.ToInt(dragedPage));
            int? parentPageID = BLL.Helper.ToInt(newParentPage);

            PageRepository pageRepository = new PageRepository();
            EasyBuildingCMS.BLL.CoreEngine.Page draggedPage = pageRepository.GetPageByID(draggedPageID);
            EasyBuildingCMS.BLL.CoreEngine.Page parentPage = draggedPage.ParentPage;

            if (parentPage != null) { 
                if (parentPage.PageID!=parentPageID)
                {
                    pageRepository.UpdateParentNode(draggedPageID, parentPageID);
                }
            }
            else if (!(parentPage == null && parentPageID == null))
            {
                pageRepository.UpdateParentNode(draggedPageID, parentPageID);
            }
            

            int i=0;
            foreach (ASTreeViewNode child in children)
            {
                i++;
                int childID = (int)(BLL.Helper.ToInt(child.NodeValue));
                pageRepository.UpdateSortOrder(childID, i);
            }

            LoadTreeView();
      
        }

        protected void LoadTreeView()
        {
            PageRepository pageRepository = new PageRepository();
            this.siteMapTreeView.DataTableRootNodeValue = null;

            ASTreeViewTheme macOS = new ASTreeViewTheme();
            macOS.BasePath = "~/ThirdPartComponents/AsTreeViewControl/astreeview/themes/macOS/";
            macOS.CssFile = "macOS.css";
            this.siteMapTreeView.Theme = macOS;

            ASTreeViewDataTableColumnDescriptor descripter = new ASTreeViewDataTableColumnDescriptor("PageNavigationText"
            , "PageID", "ParentPageID");

            this.siteMapTreeView.DataSourceDescriptor = descripter;
            this.siteMapTreeView.DataSource = pageRepository.GetPageDateTable();
            this.siteMapTreeView.DataBind();
        }
    }
}